package com.ava.avamedia;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.Toast;

import com.ava.avamedia.util.Constants;
import com.congnt.library.common.ConfigManager;

public class MainActivity extends Activity {

	protected static final String TAG = null;
	private boolean isRemember;
	private String password;
	private String userName;
	private ConfigManager config;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		config = new ConfigManager(this, true);
		isRemember = config.getBoolean(Constants.REMEMBER, false);
		Button buttonLogin = (Button) findViewById(R.id.main_btn_login);
		final EditText edtUserName = (EditText) findViewById(R.id.main_txt_user_name);
		final EditText edtPassword = (EditText) findViewById(R.id.main_txt_password);
		final CheckBox cbRemember = (CheckBox) findViewById(R.id.main_cb_remember_me);
		buttonLogin.setOnClickListener(new OnClickListener() {

			public void onClick(View v) {
				isRemember = cbRemember.isChecked();
				userName = edtUserName.getText().toString();
				password = edtPassword.getText().toString();
				if (userName.equals("") || password.equals("")) {
					Toast.makeText(MainActivity.this, "Tài khoản và mật khẩu không được để rỗng", Toast.LENGTH_SHORT).show();
				}else{
					if (password.equals(userName)) {
						if (isRemember) {
							config.putBoolean(Constants.REMEMBER, isRemember);
						}
						Intent intent = new Intent();
						intent.setClass(MainActivity.this, ListIconActivity.class);
						startActivity(intent);
						finish();

					}else{
						AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
						builder.setTitle("Error");
						builder.setMessage("Tài Khoản và mật khẩu sai");
						builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
						        public void onClick(DialogInterface dialog, int whichButton) {
						        	dialog.dismiss();
						        }
						    });
						Dialog dialog = builder.create();
						dialog.show();
					}
				}
			
			}
		});
		if (isRemember) {
			Intent intent = new Intent();
			intent.setClass(MainActivity.this, ListIconActivity.class);
			startActivity(intent);
			finish();
		}
//		showDialogInputText();
	}
	public void showDialogInputText(){
		final AlertDialog.Builder alert = new AlertDialog.Builder(this);
	    final EditText input = new EditText(this);
	    input.setRawInputType(InputType.TYPE_CLASS_PHONE);
	    alert.setTitle("Nhập số điện thoại để tự động gửi tin nhắn");
	    alert.setView(input);
	    alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int whichButton) {
	            String value = input.getText().toString().trim();
	            config.putString(Constants.SEND_SMS_TO_PHONE, value);
	            dialog.dismiss();
	        }
	    });

	    alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
	        public void onClick(DialogInterface dialog, int whichButton) {
	            dialog.cancel();
	            finish();
	        }
	    });
	    alert.show();      
	}
}
